package com.RMP
{
    import flash.display.*;
    import flash.events.*;
    import flash.net.*;
    import flash.text.*;
    import flash.utils.*;
    import com.greensock.*;

    public class TextSlideshow extends Sprite
    {
        var timerSpeed:Number;
        public var init:Boolean;
        var xmlLoader:URLLoader;
        var shortTextFormat:TextFormat;
        var fadeSpeed:Number;
        var textArray:Array;
        var curr:Number;
        var longTextFormat:TextFormat;
        var timer:Timer;
        var hRule:HorizontalRule;
        var textLoader:TextField;
        var myRoot:MovieClip;
        var currSmallText:String;
        var shortField:TextField;
        var longField:TextField;
        var theXML:XMLList;
        var texts:XMLList;
        var currLargeText:String;

        public function TextSlideshow(param1:MovieClip, param2:XMLList, param3:Number) : void
        {
            myRoot = param1;
            theXML = param2;
            fadeSpeed = param3;
            textArray = new Array();
            curr = 0;
            timerSpeed = 7500;
            timer = new Timer(timerSpeed);
            timer.addEventListener(TimerEvent.TIMER, switchText, false, 0, true);
            init = false;
            shortTextFormat = new TextFormat();
            shortTextFormat.font = new BodoniLightEmbed().fontName;
            shortTextFormat.size = 17;
            shortTextFormat.letterSpacing = -0.6;
            shortTextFormat.leftMargin = 0;
            shortTextFormat.rightMargin = 0;
            shortTextFormat.color = myRoot.coldColor;
            longTextFormat = new TextFormat();
            longTextFormat.font = new BodoniLightEmbed().fontName;
            longTextFormat.size = 62;
            longTextFormat.letterSpacing = -1;
            longTextFormat.kerning = true;
            longTextFormat.leading = -2;
            longTextFormat.leftMargin = 0;
            longTextFormat.rightMargin = 0;
            longTextFormat.color = myRoot.coldColor;
            hRule = new HorizontalRule();
            hRule.alpha = 1;
            addChild(hRule);
            getXML();
            myRoot.stage.addEventListener(Event.RESIZE, onResizer, false, 0, true);
            return;
        }

        function doResize() : void
        {
            hRule.y = Math.round(4 * myRoot.unitH + 3 * myRoot.gutterH);
            hRule.width = Math.round(myRoot.gridCurrW);
            hRule.height = 1;
            if (shortField != null)
            {
                var _loc_1:* = myRoot.unitW / 75;
                longField.scaleY = myRoot.unitW / 75;
                longField.scaleX = _loc_1;
                longField.x = -2 * longField.scaleX;
                longField.y = -10 * longField.scaleX;
                var _loc_1:* = myRoot.unitW / 75;
                shortField.scaleY = myRoot.unitW / 75;
                shortField.scaleX = _loc_1;
                shortField.x = -2 * shortField.scaleX;
                shortField.y = Math.round(4 * myRoot.unitH + 3 * myRoot.gutterH - shortField.height - 5 / 115 * myRoot.unitH);
            }
            return;
        }

        function randomizeArray(param1:Array) : Array
        {
            var _loc_2:int = 0;
            var _loc_3:* = undefined;
            var _loc_4:int = 0;
            var _loc_5:int = 0;
            _loc_2 = param1.length;
            _loc_4 = _loc_2 - 1;
            while (_loc_4 > 0)
            {
                
                _loc_5 = Math.floor(Math.random() * _loc_2);
                _loc_3 = param1[_loc_4];
                param1[_loc_4] = param1[_loc_5];
                param1[_loc_5] = _loc_3;
                _loc_4 = _loc_4 - 1;
            }
            return param1;
        }

        private function loadComplete() : void
        {
            if (shortField != null)
            {
                fadeOut();
            }
            else
            {
                fadeIn();
            }
            return;
        }

        function fadeOut() : void
        {
            TweenMax.to(shortField, fadeSpeed, {alpha:0, overwrite:true, onComplete:function ()
            {
                fadeIn();
                return;
            }
            });
            TweenMax.to(longField, fadeSpeed, {alpha:0, overwrite:true});
            return;
        }

        function onResizer(event:Event)
        {
            doResize();
            return;
        }

        function restartTimer() : void
        {
            timer.start();
            return;
        }

        private function switchText(event:TimerEvent) : void
        {
            timer.reset();
            if (curr < textArray.length)
            {
                currSmallText = textArray[curr][0];
                currLargeText = textArray[curr][1];
                var _loc_3:* = curr + 1;
                curr = _loc_3;
                loadComplete();
            }
            else
            {
                curr = 0;
                switchText(null);
            }
            return;
        }

        function updateText() : void
        {
            shortField.htmlText = currSmallText;
            shortField.setTextFormat(shortTextFormat);
            longField.htmlText = currLargeText;
            longField.setTextFormat(longTextFormat);
            return;
        }

        public function stopTween()
        {
            timer.stop();
            TweenMax.killTweensOf(shortField);
            return;
        }

        function fadeIn() : void
        {
            if (init == false)
            {
                init = true;
                shortField = new TextField();
                shortField.autoSize = "left";
                shortField.antiAliasType = AntiAliasType.ADVANCED;
                shortField.gridFitType = GridFitType.PIXEL;
                shortField.selectable = false;
                shortField.embedFonts = true;
                shortField.multiline = true;
                this.addChild(shortField);
                longField = new TextField();
                longField.width = 1009;
                longField.height = 500;
                longField.antiAliasType = AntiAliasType.ADVANCED;
                longField.gridFitType = GridFitType.PIXEL;
                longField.selectable = false;
                longField.embedFonts = true;
                longField.multiline = true;
                longField.wordWrap = true;
                this.addChild(longField);
            }
            updateText();
            doResize();
            shortField.alpha = 0;
            longField.alpha = 0;
            TweenMax.to(shortField, fadeSpeed * 4, {alpha:1, delay:fadeSpeed / 2, overwrite:true, onComplete:function ()
            {
                setTimeout(restartTimer, 1000);
                return;
            }
            });
            TweenMax.to(longField, fadeSpeed * 4, {alpha:1, overwrite:true});
            return;
        }

        private function getXML() : void
        {
            var _loc_1:int = 0;
            var _loc_2:Array = null;
            _loc_1 = 0;
            while (_loc_1 < theXML.length())
            {
                
                _loc_2 = new Array();
                _loc_2.push(theXML[_loc_1].smallTitle);
                _loc_2.push(theXML[_loc_1].largeTitle);
                textArray.push(_loc_2);
                _loc_1++;
            }
            textArray = randomizeArray(textArray);
            timer.start();
            switchText(null);
            return;
        }

    }
}
